<template>
  <view class="wrapper">
    <!-- 收货信息 -->
    <block v-if="address">
      <view class="shipment">
       <view class="dt">收货人:</view>
       <view class="dd meta">
        <text class="name">{{address.userName}}</text>
        <text class="phone">{{address.telNumber}}</text>
       </view>
       <view class="dt">收货地址:</view>
       <view class="dd">{{adr}}</view>
      </view>
    </block>
    <block v-else>
      <button @click="getAddress">获取收获地址</button>
    </block>
   
    <!-- 购物车 -->
    <view class="carts">
      <view class="item">
        <!-- 店铺名称 -->
        <view class="shopname">优购生活馆</view>

        <!-- 便利购物车商品数据 -->
        <view class="goods" :key="cart.goods_id" v-for="(cart,i) in carts">
          <!-- 商品图片 -->
          <image class="pic" :src="cart.goods_small_logo" />
          <!-- 商品信息 -->
          <view class="meta">
            <view class="name">{{cart.goods_name}}</view>
            <view class="price">
              <text>￥</text>{{cart.goods_price}}
              <text>.00</text>
            </view>
            <!-- 加减 -->
            <view class="amount">
              <text class="reduce" @click="changeCount(i,-1)">-</text>
              <input type="number" v-model="cart.goods_count" class="number" />
              <text class="plus"  @click="changeCount(i,1)">+</text>
            </view>
          </view>
          <!-- 选框 -->
          <view class="checkbox" @click="selPrd(i)">
            <icon type="success" size="20" :color="cart.goods_checked?'#ea4451':'#ccc'"></icon>
          </view>
        </view>

      </view>
    </view>
    <!-- 其它 -->
    <view class="extra"> 
      <label class="checkall" @click="checkAll">
        <icon type="success" :color="isSelAll?'#ea4451':'#ccc'" size="20"></icon>全选
      </label>
      <view class="total">
        合计:
        <text>￥</text>
        <label>{{selTotal}}</label>
        <text>.00</text>
      </view>
      <view class="pay" @click="payCarts">结算({{checkedPrds.length}})</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 购物车数据
      carts: [],

      // 收获地址
      address: null
    }
  },

  computed: {
    // 收货详细地址
    adr () {
      if (!this.address) return ''
      const {provinceName,cityName,countyName,detailInfo} = this.address
      return provinceName + cityName + countyName + detailInfo
    },

    // 购物车商品是否全部选中
    isSelAll() {
      return this.carts.every((item)=>item.goods_checked)
    },
    // 当前选中商品 --->用户下购买的
    checkedPrds() {
      return this.carts.filter((item)=>item.goods_checked)
    },

    // 计算选中的商品总价
    // 普通计算
    total() {
      let total = 0
      this.checkedPrds.forEach((item)=>{
        total += item.goods_price*item.goods_count
      })
      return total
    },
    // reduce计算
    selTotal () {
      return this.checkedPrds.reduce((acc,cur)=> acc+cur.goods_price * cur.goods_count,0)
    }
  },

  // 只执行一次
  // onLoad() {
    
  // },
  // 多次执行
  onShow() {
    this.getCarts()
  },

  methods: {
    // 创建订单---结算
    payCarts() {
      // 满足三个条件 至少选择一个商品 登录
      if (!this.address || !this.checkedPrds.length) {
        return uni.showModal({
          title:'提示',
          content:'请选择收获地址或至少选中一件商品!',
           showCancel:false
        })
      }
      if (!uni.getStorageSync('hm_token')) {
         uni.showModal({
          title:'提示',
          content:'请登录以后再创建订单',
          showCancel: false,
          success: ()=> {
            // 跳转页面
            uni.navigateTo({
          url:'/packone/auth/index'
        }) 
          }
        })
        
      }
      // 创建订单
      console.log('创建订单业务');


    },

    // 获取微信客户端登录人 收获地址
    async getAddress() {
      // uni.chooseAddress({
      //   success(res) {
      //     console.log(res);
      //   }
      // })
      const [err,address] = await uni.chooseAddress()
      console.log('获取收货地址:',address);
      if (!err) {
        this.address = address
      }
    },

    // 购物车商品单选
    selPrd(index) {
      // 根据电机时候获取的购物车商品索引 修改商品的选中状态
      this.carts[index].goods_checked = !this.carts[index].goods_checked 

      // 更新本地数据上
      this.updateCarts()
    },

    // 购物车商品全选
    checkAll() {
      if (this.isSelAll) {
        // 如果全部选中--全部未选中
        this.carts.forEach((item)=>{
          item.goods_checked = false
        })
      }else {
         this.carts.forEach((item)=>{
          item.goods_checked = true
        })
      }
      // 更新本地数据
      this.updateCarts()
    },

    // 更新本地购物车商品数据
    updateCarts(){
      uni.setStorageSync('carts',this.carts)
    },

    // 获取购物车数据=>本地获取缓存 调用接口(商品ID) 同步购物车商品数据
    getCarts() {
      this.carts = uni.getStorageSync('carts') || []
    },
    
       // 购物车加减操作
      // index 当前商品索引  step 步长 加一建议
    changeCount(index,step) {
        // 获取当前商品数量
        let count = this.carts[index].goods_count

        // 边界判断
        let kc = 12 

        // 增加购物车数量
        if (step === 1 && count>=kc) {
          return uni.showToast({
            title: `超出库存: ${kc}`,
            icon: 'none'
          })
        }else if (step === -1 && count ===1) {
          return uni.showToast({
            title:'购物车商品最小数量为1',
            icon: 'none'
          })
        }

        // 加减操作
        this.carts[index].goods_count += step

        // 更新本地缓存数据
        this.updateCarts()
      }
  }
};
</script>

<style scoped lang="scss">
.shipment {
  height: 100rpx;
  line-height: 2;
  padding: 30rpx 30rpx 40rpx 30rpx;
  font-size: 27rpx;
  color: #333;
  background-color: #fff;
  background-image: url(http://static.botue.com/ugo/images/cart_border%402x.png);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: bottom;

  .dt {
    width: 140rpx;
    float: left;
    clear: both;
  }

  .dd {
    padding-left: 160rpx;
  }

  .meta {
    padding-right: 50rpx;
  }

  text.phone {
    float: right;
  }
}

.carts {
  background-color: #f4f4f4;
  padding-bottom: 110rpx;
  overflow: hidden;

  .shopname {
    padding: 30rpx;
    margin-top: 20rpx;
    font-size: 30rpx;
    color: #333;
    background-color: #fff;
    border-top: 1rpx solid #eee;
    border-bottom: 1rpx solid #eee;
  }

  .goods {
    display: flex;
    padding: 30rpx 20rpx 30rpx 0;
    margin-left: 100rpx;
    border-bottom: 1rpx solid #eee;
    background-color: #fff;

    position: relative;

    .checkbox {
      width: 101rpx;
      height: 100%;
      background-color: #fff;

      display: flex;
      justify-content: center;
      align-items: center;

      position: absolute;
      left: -100rpx;
      top: 0;
    }

    &:last-child {
      border-bottom: none;
    }

    .pic {
      width: 200rpx;
      height: 200rpx;
      margin-right: 30rpx;
    }

    .meta {
      flex: 1;
      font-size: 27rpx;
      color: #333;
      position: relative;
    }

    .name {
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .price {
      position: absolute;
      bottom: 0;

      color: #ea4451;
      font-size: 33rpx;

      text {
        font-size: 22rpx;
      }
    }

    .amount {
      position: absolute;
      bottom: 0;
      right: 20rpx;

      height: 48rpx;
      text-align: center;
      border: 1rpx solid #ddd;
      border-radius: 8rpx;

      display: flex;
      align-items: center;

      text {
        display: block;
        width: 60rpx;
        line-height: 48rpx;
        font-size: 36rpx;
        color: #ddd;
        text-align: center;
      }

      input {
        width: 60rpx;
        height: 48rpx;
        min-height: 48rpx;
        font-size: 27rpx;
        border-left: 1rpx solid #ddd;
        border-right: 1rpx solid #ddd;
      }
    }
  }
}

.extra {
  position: fixed;
  bottom: 0;
  /* #ifdef H5 */
  bottom: 50px;
  /* #endif */
  left: 0;
  z-index: 9;

  width: 750rpx;
  height: 96rpx;
  text-align: center;
  line-height: 96rpx;
  font-size: 36rpx;
  border-top: 1rpx solid #eee;
  background-color: #fff;
  color: #333;
  display: flex;

  .checkall {
    width: 140rpx;
    line-height: 1;
    margin-left: 25rpx;
    display: flex;
    align-items: center;

    icon {
      margin-right: 20rpx;
    }
  }

  .total {
    display: flex;
    justify-content: center;
    flex: 1;

    label,
    text {
      color: #ea4451;
      vertical-align: bottom;
      position: relative;
      bottom: -2rpx;
    }

    text {
      position: relative;
      bottom: -3rpx;
      font-size: 24rpx;

      &:first-child {
        margin-left: 10rpx;
      }
    }
  }

  .pay {
    width: 200rpx;
    background-color: #ea4451;
    color: #fff;
  }
}
</style>

